package com.xxx.dorm.dao.impl;

import com.xxx.dorm.dao.UserDao;
import com.xxx.dorm.domain.Role;
import com.xxx.dorm.domain.Student;
import com.xxx.dorm.domain.Users;
import com.xxx.dorm.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class UserDaoImpl implements UserDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public Users findByusernameAndPassword(String username, String password) {
        String sql = "select * from t_user where username = ? and password = ?";
        Users users = null;
        try {
            users = template.queryForObject(sql, new BeanPropertyRowMapper<>(Users.class), username, password);
            return users;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public void updatePassword(Integer id, String password) {
        String sql = "update t_user set password = ? where id = ?";
        template.update(sql,password,id);
    }

    /**
     * 根据rid获取角色表
     * @param rid
     * @return
     */
    @Override
    public Role findByRid(Integer rid) {
        String sql = "SELECT * FROM t_role WHERE id = ?";
        Role role = template.queryForObject(sql,new BeanPropertyRowMapper<>(Role.class),rid);
        return role;
    }

    /**
     * 查找所有用户信息
     * @param userid
     * @return
     */
    @Override
    public List<Users> findAll(Integer userid) {
        String sql = "select * from t_user where id != ?";
        return template.query(sql,new BeanPropertyRowMapper<>(Users.class),userid+"");
    }

    /**
     * 删除用户
     * @param id
     */
    @Override
    public void removeUser(String id) {
        String sql = "delete from t_user where id = ? AND id != '1' ";
        template.update(sql,id);
    }

    @Override
    public Users findByid(String id) {
        String sql ="select * from t_user where id = ?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(Users.class),id);
    }

    /**
     * 通过条件查找用户信息
     * @param conditions
     * @param information
     * @return
     */
    @Override
    public List<Users> find(String conditions, String information) {
        String sql = null;
        if(conditions.equals("sex")){
            sql = "select * from t_user where sex = ? AND id != '1'";
        }else if(conditions.equals("name")){
            sql = "select * from t_user where name = ? AND id != '1'";
        }else if(conditions.equals("phone")){
            sql = "select * from t_user where phone = ? AND id != '1'";
        }
        return template.query(sql,new BeanPropertyRowMapper<>(Users.class),information+"");
    }

    @Override
    public void addUser(Users users) {
        String sql = "insert into t_user(username,password,name,sex,phone,rid) values(?,?,?,?,?,?)";
        template.update(sql,users.getUsername(),users.getPassword(),users.getName(),users.getSex(),users.getPhone(),2);
    }

    @Override
    public void updateUser(Users users) {
        String sql = "update t_user set username = ?,password = ?,name = ?,sex = ?,phone = ? where id = ?";
        template.update(sql,users.getUsername(),users.getPassword(),users.getName(),users.getSex(),users.getPhone(),users.getId());
    }

    @Override
    public Users findOldPassword(String oldPassword) {
        String sql = "select * from t_user where password = ?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(Users.class),oldPassword);
    }
}
